#!/usr/bin/perl
use strict;
package nsidentify;

sub nsidentify {
    use Digest::MD5 qw(md5_base64);
    my ($nsnum,$dbh,$remote,$input,$ns2) = @_;
#get passwd...
    my $sth=$dbh->prepare("SELECT password FROM nicks WHERE rnick = '$$input[4]';");
    $sth->execute;
    my $realpass = "";
    my $realpass = $sth->fetchrow_array;

#GET HIS REAL NICK.
    $sth=$dbh->prepare("SELECT num,nick,host,auth from ninfo WHERE num = '$$input[0]';");
    $sth->execute;
    my @tmp;
    my $nick;
    my $host;
    my $isauth;
    while (@tmp = $sth->fetchrow_array) {
	if ($tmp[0] eq $$input[0]) {
	    $nick = $tmp[1];
	    $host = $tmp[2];
	    $isauth = $tmp[3];
	}
    }
    undef @tmp;
    if ($$input[5] eq "") {
	print $remote "$nsnum O $$input[0] :Syntax :IDENTIFY <Rnick> <Password>\n";
    }
    elsif (lc($$input[2]) ne lc($ns2)) {
	print $remote "$nsnum O $$input[0] :Please use /MSG $ns2 identify <rnick> <password>\n";
    }
    elsif ($isauth ne '0') {
	print $remote "$nsnum O $$input[0] :You are already identified as \cB$isauth\cB.\n";
    }
    elsif ($realpass eq "") {
        print $remote "$nsnum O $$input[0] :This nickname is not registered.\n";
    }

    elsif ($realpass ne md5_base64($$input[5])) {
	print $remote "$nsnum O $$input[0] :Incorrect password.\n";
    }
    else {
	my $time = time;
	$sth=$dbh->prepare("UPDATE ninfo SET auth = '$$input[4]' WHERE num = '$$input[0]' AND nick = '$nick';");
	$sth->execute;
	$sth=$dbh->prepare("UPDATE nicks SET lasthost = '$host', lastseen = '$time';");
	$sth->execute;
	undef $time;
	print $remote "$nsnum O $$input[0] :Identified as \cB$$input[4]\cB.\n";
    }
    undef $nick;
    undef $host;
    undef $realpass;
}
1;
